package com.share.design.composite.tree;

import java.util.ArrayList;
import java.util.List;

/**
 * 组合节点
 * @author caifan
 * @since 2022/10/30
 */
public class Node extends AbstractNode {

    /**
     * 子节点
     */
    private List<AbstractNode> children;

    /**
     * 自己id
     */
    private int id;

    /**
     * 父id
     */
    private int pid;

    public Node() {
        children = new ArrayList<AbstractNode>();
    }

    @Override
    public boolean isRoot() {
        return 0 == pid;
    }

    @Override
    public int getId() {
        return this.id;
    }

    @Override
    public int getParentId() {
        return this.pid;
    }

    @Override
    public void setId(int id) {
        this.id = id;
    }

    @Override
    public void setParentId(int parentId) {
        this.pid = parentId;
    }

    @Override
    public void add(AbstractNode abstractNode) {
        abstractNode.setParentId(this.pid + children.size());
        abstractNode.setId(abstractNode.getParentId() + 1);
        children.add(abstractNode);
    }

    @Override
    public void remove(AbstractNode g) {
        children.remove(g);
    }

    @Override
    public AbstractNode getChild(int i) {
        return children.get(i);
    }
}
